<?php
$dbh = new PDO('mysql:host=localhost;dbname=blog', 'root', '');
// Check if a blog title already exists
function blogExists($dbh, $blogTitle) {
    $sql = "SELECT COUNT(*) AS cnt FROM blogList WHERE blogTitle=".$dbh->quote($blogTitle);		
		$row = $dbh->query($sql)->fetch();
    return !empty($row['cnt' ]);
}
// Insert a new blog into the list
function deleteBlog($dbh, $blogTitle) {
    $sql = "DELETE FROM blogList WHERE blogTitle = '$_POST[blogTitle]'";
		echo htmlentities($sql, ENT_QUOTES);
		return $dbh->exec($sql);
}
// Get list of blogs
function getBlog($dbh) {
    $sql = "SELECT * FROM blogList";
    $blogs = $dbh->query($sql)->fetchAll();
    return $blogs;
}

$blog = getBlog($dbh);
$error = '' ;
$success = '' ;
$blog = isset($_POST['bTitle' ]) ? $_POST['bTitle' ] : '' ;
if ($_SERVER['REQUEST_METHOD' ] == 'POST' ) {
	  if (deleteBlog($dbh, $blogTitle)) {
        $success = "blog has been deleted.";
		}
    elseif (!blogExists($dbh, $blogTitle)) {
        $error = "Blog Title does not exist.";
    }
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Delete Blog</title>
<meta http-equiv="refresh" content="0;url=http://localhost/listBlogs.php">
</head>
<body>
<br/><br/>
<a href="/blog_Home.php">
<img src="Images/home_button.gif" alt="Blog Page"/></a>
</body>
</html>

